#!groovy
@Library('jenkinslabrary@master') _
def tools = new org.efssc.tools()
def getenv = new org.efssc.getmetadata()
def getdockertag = new org.efssc.imagetag()
def getbuild = new org.efssc.build()
def unittest = new org.efssc.unittest()
def sonarqube = new org.efssc.sonarqube()
def nexus = new org.efssc.nexues()
def buildimage = new org.efssc.builddocker()
def sshserver = new org.efssc.sshagent()
pipeline {
  agent {
    node {
      label 'maven'
    }
  }
  parameters {
    string(name:'APPNAME',defaultValue: 'sentinel-dashboard',description:'发布服务名称')
    string(name:'TAG_NAME',defaultValue: 'release-1.8.1-galaxy',description:'发布分支名称')
    choice(name: 'PUBLISH_ENV', choices: ['dev', 'test', 'uat'], description: '发布环境选择')
   }
  environment {
     GIT_URL="https://gitlab.fosun.com/assistfc/middleware/alibaba/Sentinel.git"
     GIT_DEVOPS="https://gitlab.fosun.com/assistfc/devops/manager/scripts/jenkins-pipeline.git"
     GIT_CREDENTIALSID_NAME="github-id"
     SONAR_CREDENTIALSID_NAME="sonar-token"
     SONAR_SERVER="http://10.41.1.42:32716"  
     NEXUES_SERVER="https://nexus.fosun.com/repository/galaxy-nexus-releases" 
     DOCKER_CREDENTIALSID_NAME=" dockerhub-id"
     // DOCKER_CREDENTIALSID_NAME="harbor-id"
     REGISTRY="harbor.assistfc.com"
      SSH_CREDENTIALSID_NAME="dev-ssh"
  }
  stages {
    stage('gitclone代码') {
        steps {
            dir("devops") {
                git(url: "${GIT_DEVOPS}", credentialsId: "${GIT_CREDENTIALSID_NAME}", branch: "master", changelog: true, poll: false)
                script {
                    tools.PrintMes("gitclone code and get cicd environment", "red")
                    getenv.GetMetadata("${params.APPNAME}", "${params.TAG_NAME}")
                }
                sh "echo ${appname}"
            }
            dir("${appname}") {
                git(url: "${appgiturl}", credentialsId: "${GIT_CREDENTIALSID_NAME}", branch: "${appbranch}", changelog: true, poll: false)
                script {
                        getdockertag.getimagetag("${appversion}")
                    }
                    sh "echo ${image_name}"
                }
            }
        }
    stage('code_check'){
      steps{
        container("${container}"){
            script {
                unittest.testjar("${apptype}","${appname}")
            }
        }
      }
    }
    stage('sonarqube'){
      steps{
        container("${container}"){
          withCredentials([string(credentialsId : "${SONAR_CREDENTIALSID_NAME}" ,variable : 'sonartoken' ,)]) {
            script {
              sonarqube.Sonarplugin("${SONAR_SERVER}","${sonartoken}","${apptype}","${appname}")
            }  
        }
      }
    }
    }
    stage('uploadjarnexus'){
      steps{
          script{
            nexus.main("maven","${appname}","${NEXUES_SERVER}") 
          }
      }
    } 
    stage('build&&push'){
      steps{
        container("${container}") {
            withCredentials([usernamePassword(credentialsId : "${DOCKER_CREDENTIALSID_NAME}" ,passwordVariable : 'Habor_passwd' ,usernameVariable : 'Habor_username' ,)]) {
                script {
                    def jarapp = ["galaxy-boot","galaxy-framework","galaxy-cloud"]
                    result = jarapp.any{it == "${appname}"}
                    if ("${result}" == true ) {
                        echo "不许要执行，只上传jar包操作"
                    } else {
                        buildimage.build("${apptype}", "${appname}", "${REGISTRY}", "${params.PUBLISH_ENV}", "${image_name}", "${Habor_passwd}", "${Habor_username}")
                    }
                }
            }
        }
      
    }
  }
      stage("deployapp"){
          steps{
              script{
                  def jarapp = ["galaxy-boot","galaxy-framework","galaxy-cloud"]
                  result = jarapp.any{it == "${appname}"}
                  if ("${result}" == true ) {
                      echo "不许要执行，只上传jar包操作"
                  } else {
                      server_name = sshserver.main("${SSH_CREDENTIALSID_NAME}", "${appname}", "${namespace}", "${image_name}", "${params.PUBLISH_ENV}")
                  }
              }
          }
      }
  
}
}
